from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter
from drf_haystack.viewsets import HaystackViewSet

from goods.models import SKU
from .serializers import SKUSerializer, SKUIndexSerializer


# Create your views here.


class SKUListview(ListAPIView):
    """
    sku列表数据
    """
    serializer_class = SKUSerializer
    # 字段过滤
    filter_backends = [OrderingFilter]
    # 排序
    ordering_fields = ('create_time','price','sales')

    def get_queryset(self):
        # 在视图对象的kwargs读取路径参数
        category_id = self.kwargs['category_id']
        # categor-id对sku数据进行过滤，保持查询到的sku信息都是指定的category-id所在的分类
        return SKU.objects.filter(category_id=category_id,is_launched=True)


class SKUSerachViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer